<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="子订单编号" prop="subOrderNo">
        <el-input
          v-model="queryParams.subOrderNo"
          placeholder="请输入子订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单时间" prop="orderTime">
        <el-date-picker clearable
          v-model="queryParams.orderTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择下单时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="商品ID" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入商品ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品识别码/SKU" prop="productSpec">
        <el-input
          v-model="queryParams.productSpec"
          placeholder="请输入商品识别码/SKU"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发货状态" prop="deliveryStatus">
        <el-select v-model="queryParams.deliveryStatus" placeholder="请选择发货状态" clearable>
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="是否有进行中的退款1是0否 " prop="refundIng">
        <el-input
          v-model="queryParams.refundIng"
          placeholder="请输入是否有进行中的退款1是0否 "
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="收货人姓名" prop="receiver">
        <el-input
          v-model="queryParams.receiver"
          placeholder="请输入收货人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人电话" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入收货人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家" prop="merchantName">
        <el-input
          v-model="queryParams.merchantName"
          placeholder="请输入商家"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="merchantEmail">
        <el-input
          v-model="queryParams.merchantEmail"
          placeholder="请输入邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮件状态" prop="mailSent">
        <el-select v-model="queryParams.mailSent" placeholder="请选择邮件状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_eamil_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="邮件发送时间">
        <el-date-picker
          v-model="daterangeMailSentTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="邮件回传时间">
        <el-date-picker
          v-model="daterangeMailReturnTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--    <el-col :span="1.5">
       <el-button
         type="primary"
         plain
         icon="el-icon-plus"
         size="mini"
         @click="handleAdd"
         v-hasPermi="['zhongchuangauto:zcmasterorder:add']"
       >新增</el-button>
     </el-col>
     <el-col :span="1.5">
       <el-button
         type="success"
         plain
         icon="el-icon-edit"
         size="mini"
         :disabled="single"
         @click="handleUpdate"
         v-hasPermi="['zhongchuangauto:zcmasterorder:edit']"
       >修改</el-button>
     </el-col>
      <el-col :span="1.5">
             <el-button
               type="danger"
               plain
               icon="el-icon-delete"
               size="mini"
               :disabled="multiple"
               @click="handleDelete"
               v-hasPermi="['zhongchuangauto:zcmasterorder:remove']"
             >删除</el-button>
           </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['zhongchuangauto:zcmasterorder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="zcmasterorderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="orderId" />
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="快递公司" align="center" prop="dictExpress" />
      <el-table-column label="快递单号" align="center" prop="expressNo" />
      <el-table-column label="包裹中商品件数" align="center" prop="packageQty" />
      <el-table-column label="子订单编号" align="center" prop="subOrderNo" />
      <el-table-column label="下单时间" align="center" prop="orderTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品ID" align="center" prop="productCode" />
      <el-table-column label="商品名称" align="center" prop="productName" />
      <el-table-column label="规格信息" align="center" prop="specInfo" />
      <el-table-column label="商品识别码/SKU" align="center" prop="productSpec" />
      <el-table-column label="购买件数" align="center" prop="quantity" />
      <el-table-column label="兑换单价(元)" align="center" prop="unitPrice" />
      <el-table-column label="商品总价(元)" align="center" prop="totalPrice" />
      <el-table-column label="优惠券(元)" align="center" prop="couponAmt" />
      <el-table-column label="商品实付(元)" align="center" prop="realPay" />
      <el-table-column label="发货状态" align="center" prop="deliveryStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.deliveryStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="是否有进行中的退款1是0否 " align="center" prop="refundIng" />
      <el-table-column label="收货人姓名" align="center" prop="receiver" />
      <el-table-column label="收货人电话" align="center" prop="mobile" />
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="区县" align="center" prop="district" />
      <el-table-column label="详细地址" align="center" prop="detail" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="商家" align="center" prop="merchantName" />
      <el-table-column label="邮箱" align="center" prop="merchantEmail" />
      <el-table-column label="邮件状态" align="center" prop="mailSent">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_eamil_status" :value="scope.row.mailSent"/>
        </template>
      </el-table-column>
      <el-table-column label="邮件发送时间" align="center" prop="mailSentTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.mailSentTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="邮件回传时间" align="center" prop="mailReturnTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.mailReturnTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['zhongchuangauto:zcmasterorder:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['zhongchuangauto:zcmasterorder:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单主表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="快递公司" prop="dictExpress">
          <el-input v-model="form.dictExpress" placeholder="请输入快递公司" />
        </el-form-item>
        <el-form-item label="快递单号" prop="expressNo">
          <el-input v-model="form.expressNo" placeholder="请输入快递单号" />
        </el-form-item>
        <el-form-item label="包裹中商品件数" prop="packageQty">
          <el-input v-model="form.packageQty" placeholder="请输入包裹中商品件数" />
        </el-form-item>
        <el-form-item label="子订单编号" prop="subOrderNo">
          <el-input v-model="form.subOrderNo" placeholder="请输入子订单编号" />
        </el-form-item>
        <el-form-item label="下单时间" prop="orderTime">
          <el-date-picker clearable
            v-model="form.orderTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择下单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="商品ID" prop="productCode">
          <el-input v-model="form.productCode" placeholder="请输入商品ID" />
        </el-form-item>
        <el-form-item label="商品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="规格信息" prop="specInfo">
          <el-input v-model="form.specInfo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商品识别码/SKU" prop="productSpec">
          <el-input v-model="form.productSpec" placeholder="请输入商品识别码/SKU" />
        </el-form-item>
        <el-form-item label="购买件数" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入购买件数" />
        </el-form-item>
        <el-form-item label="兑换单价(元)" prop="unitPrice">
          <el-input v-model="form.unitPrice" placeholder="请输入兑换单价(元)" />
        </el-form-item>
        <el-form-item label="商品总价(元)" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入商品总价(元)" />
        </el-form-item>
        <el-form-item label="优惠券(元)" prop="couponAmt">
          <el-input v-model="form.couponAmt" placeholder="请输入优惠券(元)" />
        </el-form-item>
        <el-form-item label="商品实付(元)" prop="realPay">
          <el-input v-model="form.realPay" placeholder="请输入商品实付(元)" />
        </el-form-item>
        <el-form-item label="发货状态" prop="deliveryStatus">
          <el-radio-group v-model="form.deliveryStatus">
            <el-radio
              v-for="dict in dict.type.order_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否退款 " prop="refundIng">
          <el-input v-model="form.refundIng" placeholder="请输入是否有进行中的退款1是0否 " />
        </el-form-item>
        <el-form-item label="收货人姓名" prop="receiver">
          <el-input v-model="form.receiver" placeholder="请输入收货人姓名" />
        </el-form-item>
        <el-form-item label="收货人电话" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入收货人电话" />
        </el-form-item>
        <el-form-item label="省" prop="province">
          <el-input v-model="form.province" placeholder="请输入省" />
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-input v-model="form.city" placeholder="请输入市" />
        </el-form-item>
        <el-form-item label="区县" prop="district">
          <el-input v-model="form.district" placeholder="请输入区县" />
        </el-form-item>
        <el-form-item label="详细地址" prop="detail">
          <el-input v-model="form.detail" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商家" prop="merchantName">
          <el-input v-model="form.merchantName" placeholder="请输入商家" />
        </el-form-item>
        <el-form-item label="邮箱" prop="merchantEmail">
          <el-input v-model="form.merchantEmail" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="邮件状态" prop="mailSent">
          <el-select v-model="form.mailSent" placeholder="请选择邮件状态">
            <el-option
              v-for="dict in dict.type.sys_eamil_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="邮件发送时间" prop="mailSentTime">
          <el-date-picker clearable
            v-model="form.mailSentTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择邮件发送时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="邮件回传时间" prop="mailReturnTime">
          <el-date-picker clearable
            v-model="form.mailReturnTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择邮件回传时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listZcmasterorder, getZcmasterorder, delZcmasterorder, addZcmasterorder, updateZcmasterorder } from "@/api/zhongchuangauto/zcmasterorder";

export default {
  name: "Zcmasterorder",
  dicts: ['sys_eamil_status', 'order_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单主表表格数据
      zcmasterorderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 邮件回传时间时间范围
      daterangeMailSentTime: [],
      // 邮件回传时间时间范围
      daterangeMailReturnTime: [],
      // 邮件回传时间时间范围
      daterangeCreateTime: [],
      // 邮件回传时间时间范围
      daterangeUpdateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        subOrderNo: null,
        orderTime: null,
        productCode: null,
        productName: null,
        specInfo: null,
        productSpec: null,
        deliveryStatus: null,
        refundIng: null,
        receiver: null,
        mobile: null,
        merchantName: null,
        merchantEmail: null,
        mailSent: null,
        mailSentTime: null,
        mailReturnTime: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNo: [
          { required: true, message: "订单编号不能为空", trigger: "blur" }
        ],
        subOrderNo: [
          { required: true, message: "子订单编号不能为空", trigger: "blur" }
        ],
        orderTime: [
          { required: true, message: "下单时间不能为空", trigger: "blur" }
        ],
        productCode: [
          { required: true, message: "商品ID不能为空", trigger: "blur" }
        ],
        productName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
        productSpec: [
          { required: true, message: "商品识别码/SKU不能为空", trigger: "blur" }
        ],
        quantity: [
          { required: true, message: "购买件数不能为空", trigger: "blur" }
        ],
        unitPrice: [
          { required: true, message: "兑换单价(元)不能为空", trigger: "blur" }
        ],
        totalPrice: [
          { required: true, message: "商品总价(元)不能为空", trigger: "blur" }
        ],
        realPay: [
          { required: true, message: "商品实付(元)不能为空", trigger: "blur" }
        ],
        receiver: [
          { required: true, message: "收货人姓名不能为空", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "收货人电话不能为空", trigger: "blur" }
        ],
        province: [
          { required: true, message: "省不能为空", trigger: "blur" }
        ],
        city: [
          { required: true, message: "市不能为空", trigger: "blur" }
        ],
        district: [
          { required: true, message: "区县不能为空", trigger: "blur" }
        ],
        detail: [
          { required: true, message: "详细地址不能为空", trigger: "blur" }
        ],
        merchantName: [
          { required: true, message: "商家不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单主表列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeMailSentTime && '' != this.daterangeMailSentTime) {
        this.queryParams.params["beginMailSentTime"] = this.daterangeMailSentTime[0];
        this.queryParams.params["endMailSentTime"] = this.daterangeMailSentTime[1];
      }
      if (null != this.daterangeMailReturnTime && '' != this.daterangeMailReturnTime) {
        this.queryParams.params["beginMailReturnTime"] = this.daterangeMailReturnTime[0];
        this.queryParams.params["endMailReturnTime"] = this.daterangeMailReturnTime[1];
      }
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeUpdateTime && '' != this.daterangeUpdateTime) {
        this.queryParams.params["beginUpdateTime"] = this.daterangeUpdateTime[0];
        this.queryParams.params["endUpdateTime"] = this.daterangeUpdateTime[1];
      }
      listZcmasterorder(this.queryParams).then(response => {
        this.zcmasterorderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        orderNo: null,
        dictExpress: null,
        expressNo: null,
        packageQty: null,
        subOrderNo: null,
        orderTime: null,
        productCode: null,
        productName: null,
        specInfo: null,
        productSpec: null,
        quantity: null,
        unitPrice: null,
        totalPrice: null,
        couponAmt: null,
        realPay: null,
        deliveryStatus: null,
        refundIng: null,
        receiver: null,
        mobile: null,
        province: null,
        city: null,
        district: null,
        detail: null,
        remark: null,
        merchantName: null,
        merchantEmail: null,
        mailSent: null,
        mailSentTime: null,
        mailReturnTime: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeMailSentTime = [];
      this.daterangeMailReturnTime = [];
      this.daterangeCreateTime = [];
      this.daterangeUpdateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单主表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getZcmasterorder(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单主表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateZcmasterorder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addZcmasterorder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认删除订单主表编号为"' + orderIds + '"的数据项？').then(function() {
        return delZcmasterorder(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zhongchuangauto/zcmasterorder/export', {
        ...this.queryParams
      }, `zcmasterorder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
